//
// Created by 11510 on 2024/6/12.
//
#include <iostream>
#include <string>
using namespace std;

// 前向声明
class Complex;

class Point{
public:

    explicit
    Point(int x = 0, int y = 0):_ix(x), _iy(y){}

    string toString() const{
        return "x = " + to_string(_ix) + ", y = "+ to_string(_iy);
    }

    /**
     * 拷贝构造
     * @param rhs
     */
//    Point(const Point& rhs):_ix(rhs._ix), _iy(rhs._iy){
//        printf("copy Point()\n");
//    }
    Point(const Complex& rhs);

    /**
     * 重载赋值运算符
     * @param rhs
     * @return
     */
    Point& operator=(const Point &rhs){
        printf("operator=(Point)\n");
        this->_ix = rhs._ix;
        this->_iy = rhs._iy;
        return *this;
    }

    Point& operator=(int x){
        printf("operator=(int)\n");
        this->_ix = x;
        return *this;
    }

    Point& operator=(const Complex& rhs);

    operator int(){
        return _ix + _iy;
    }


private:
    int _ix;
    int _iy;
};




class Complex{
public:
    Complex(int real, int image):_real(real), _image(image){}

    /**
     * 逆天
     * @return
     */
    operator Point(){
        return Point(_real, _image);
    }

    friend class Point;
private:
    int _real;
    int _image;
};

/**
 * 逆天
 * @param rhs
 */
Point::Point(const Complex &rhs) {
    _ix = rhs._real;
    _iy = rhs._image;
}


Point& Point::operator=(const Complex& rhs){
    this->_ix = rhs._real;
    this->_iy = rhs._image;
}



#if 0
void test0(){
    /**
     * 隐式转换，相当于Point point = Point(1);
     */
     Point point = 1;
     cout << point.toString() << endl;

     int num = point;
     int num2 = point.operator int();// 本质
     cout << num << endl;
}


void test1(){
    Point point(3, 4);
    point = 1;

    cout << point.toString() << endl;
}
#endif


void test2(){
    Point point(4, 7);
    Complex complex(7, 9);

    /**
     * 1. 让Complex类对象complex转换成一个Pointduix
     * 2. 隐式转换思路，提供特定的构造函数
     * 3.
     */
    point = complex;
    cout << point.toString() << endl;
}
int main() {
    test2();
    return 0;
}